package com.neuedu.io;

import java.io.*;

/**
 * @author 金山
 * 项目：javase
 * site: https://blog.fulfill.com.cn
 * 描述
 * @data 2024/10/319:06
 */
public class BufferedIO {


    /**
     * 带缓冲区的读写
     *   读的过程 input
     *      1 构造 InputStream 对象   --new  FileInputStream(File)
     *             带缓冲区 new BufferedInputStream(new FileInputStream(readFile));
     *      2  读
     *      3 close
     *   写的过程 output
     *      1 构造 OutputStream 对象   --new  FileOutputStream(File)
     *             带缓冲区 new BufferedOutputStream(new FileOutputStream(writeFile));
     *      2  读
     *      3 close
     *
     *
     * @param args
     */
    public static void main(String[] args) {

        File readFile = new File("D:\\upload\\西游记.txt");
        File writeFile = new File("D:\\upload\\西游记备份.txt");

        BufferedInputStream is = null;
        BufferedOutputStream os = null;
        try {
            is = new BufferedInputStream(new FileInputStream(readFile));
            os = new BufferedOutputStream(new FileOutputStream(writeFile),2000);


            int len = -1;
            byte data[]= new byte[100];
            while (  (len = is.read(data))  != -1    ){


                //当缓冲区的内容管放不下的时候 会 刷新缓冲区
                os.write(data,0,len);

                //手动的刷新缓冲区
               os.flush();

            }



        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(is != null){
                try {
                    is.close();
                } catch (IOException e) {
                   e.printStackTrace();
                }

            }
            if(os != null){
                try {

                    //close 会刷新缓冲区
                    os.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }


    }



}
